﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FundHelper.Model
{
    public static partial class Indicator
    {
        public static IEnumerable<EmaResult> GetEmaResult(this IEnumerable<(DateTime date, double price)> list, int period)
        {
            var tuples = list.ToList();
            if (tuples.Count < period)
                throw new ArgumentException("Not enough data points for the specified EMA period.");
            var emaArry=new EmaResult[tuples.Count-period];
            var temp = tuples.OrderBy(a => a.date).ToList();
            double ema = temp.Take(period).Select(a=>a.price).Average(); // 使用简单平均作为初始值

            for (int i = period; i < temp.Count; i++)
            {
                double t1 = 2f / (period + 1);
                double t2 = 1f - t1;
                ema = temp[i].price * t1 + ema * t2;
                emaArry[i - period] = new EmaResult(temp[i].date, ema);
            }
            return emaArry.AsEnumerable();
        }
    }
    public class EmaResult
    {
        public DateTime dateTime { get; init; }
        private double value;

        [IndicatorName("ema")]
        public double Value { get => value; set => this.value = value; }
        public EmaResult(DateTime dateTime, double value)
        {
            this.dateTime = dateTime; 
            this.value = value;
        }
    }
    public class SmaResult
    {
        public DateTime dateTime { get; init; }
        private double value;
        public double Value { get => value; set => this.value = value; }
        public SmaResult(DateTime dateTime, double value)
        {
            this.dateTime = dateTime;
            this.value = value;
        }
    }
}
